package com.fulihui.usercore.biz.conv;


import com.fulihui.usercore.dal.dataobj.UserAddressDO;
import com.fulihui.usercore.dto.UserAddressDTO;
import com.fulihui.usercore.enums.BooleanValueEnum;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.fulihui.usercore.common.util.DataSafe.*;

/**
 * 地址转换工具，用 Spring 管理单例
 *
 * @author Willard.Hu on 2016/5/24.
 */
@Component
public class UserAddressConv {

    /**
     * 数据加密的 AES KEY
     */
    @Value("${data.safe.aeskey}")
    private String dataSafeAesKey;

    public String getDataSafeAesKey() {
        return dataSafeAesKey;
    }

    private static final String[] ignore = {"defaultAddr"};

    public UserAddressDO toDO(UserAddressDTO dto) {
        if (dto == null) {
            return null;
        }
        UserAddressDO uaDO = new UserAddressDO();
        BeanUtils.copyProperties(dto, uaDO);

        if (StringUtil.isNotBlank(uaDO.getConsigneeName())) {
            uaDO.setConsigneeName(aesEncrypt(uaDO.getConsigneeName(), dataSafeAesKey));
        }
        if (StringUtil.isNotBlank(uaDO.getConsigneeMobile())) {
            uaDO.setConsigneeMobile(aesEncrypt(uaDO.getConsigneeMobile(), dataSafeAesKey));
        }
        if (StringUtil.isNotBlank(uaDO.getConsigneeTel())) {
            uaDO.setConsigneeTel(aesEncrypt(uaDO.getConsigneeTel(), dataSafeAesKey));
        }
        if (StringUtil.isNotBlank(uaDO.getConsigneeAddr())) {
            Optional<String> optional = baseEncode(uaDO.getConsigneeAddr());
            uaDO.setConsigneeAddr(optional.isPresent() ? optional.get() : null);
        }

        uaDO.setDefaultAddr(BooleanValueEnum.byBoolean(dto.isDefaultAddr()).getCode());
        return uaDO;
    }

    public UserAddressDTO toDTO(UserAddressDO uaDO) {
        if (uaDO == null) {
            return null;
        }
        UserAddressDTO dto = new UserAddressDTO();
        BeanUtils.copyProperties(uaDO, dto);

        if (StringUtil.isNotBlank(dto.getConsigneeName())) {
            dto.setConsigneeName(autoDecrypt(dto.getConsigneeName()));
        }
        if (StringUtil.isNotBlank(dto.getConsigneeMobile())) {
            dto.setConsigneeMobile(autoDecrypt(dto.getConsigneeMobile()));
        }
        if (StringUtil.isNotBlank(dto.getConsigneeTel())) {
            dto.setConsigneeTel(autoDecrypt(dto.getConsigneeTel()));
        }
        if (StringUtil.isNotBlank(dto.getConsigneeAddr())) {
            dto.setConsigneeAddr(autoDecrypt(dto.getConsigneeAddr()));
        }

        dto.setDefaultAddr(BooleanValueEnum.byCode(uaDO.getDefaultAddr()).isTrue());
        return dto;
    }

    public List<UserAddressDTO> toList(List<UserAddressDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return Collections.emptyList();
        }
        return doList.stream().map(this::toDTO).collect(Collectors.toList());
    }

}
